﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

using ESOL.Common.Entity;
using ESOL.Business.Fa;

namespace ESOL.WinUI.Fa
{
    public partial class frmMaintainAssetQuery : BaseForm.frmBaseDialogyQueryForm
    {
        #region Variable
        private FaAssetsEntity mSelectedAsset;
        private FaAssetsTypeEntity mSelectedAssetType;

        public FaAssetsEntity SelectedAsset 
        {
            get 
            {
                return mSelectedAsset; 
            } 
        }
        #endregion

        #region Construction
        public frmMaintainAssetQuery()
        {
            InitializeComponent();
        }
        public frmMaintainAssetQuery(FaAssetsTypeEntity productType)
        {
            InitializeComponent();
            mSelectedAssetType = productType;
        }
        #endregion

        #region Function
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "FAS", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(repositoryItemGridLookUpEdit1, "FAS", true);
        }
        private string GetCondition()
        {
            string condition = "";
            if (txtAssetType.Tag != null && txtAssetType.Tag.GetType() == typeof(FaAssetsTypeEntity))
            {
                FaAssetsTypeEntity type = (FaAssetsTypeEntity)txtAssetType.Tag;
                condition += " AND (AT1.ASSET_TYPE_ID = '" + type.AssetTypeId + "' OR AT2.ASSET_TYPE_ID = '" + type.AssetTypeId + "' OR AT3.ASSET_TYPE_ID = '" + type.AssetTypeId + "' OR AT4.ASSET_TYPE_ID = '" + type.AssetTypeId + "' OR AT5.ASSET_TYPE_ID = '" + type.AssetTypeId + "') ";
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND FA.STATUS = '" + cmbStatus.SelectedValue.ToString() + "' ";
            }
            if (string.IsNullOrEmpty(txtAssetCode.Text) == false)
            {
                condition += " AND FA.ASSET_CODE LIKE '%" + txtAssetCode.Text + "%' ";
            }
            if (string.IsNullOrEmpty(txtAssetSpec.Text) == false)
            {
                condition += " AND FA.ASSET_SPEC = '" + txtAssetSpec.Text + "' ";
            }
            if (string.IsNullOrEmpty(txtAssetName.Text) == false)
            {
                condition += " AND FA.ASSET_NAME LIKE '%" + txtAssetName.Text + "%' ";
            }
            return condition;
        }
        private void BindDataGrid()
        {
            string condition = GetCondition();
            string orderBy = "AT2.ASSET_TYPE_NAME, AT1.ASSET_TYPE_NAME, FA.ASSET_CODE";
            DataTable dt = FaAssetsBs.GetByCondition(condition, orderBy, "");
            gvMain.DataSource = dt;
        }
        #endregion

        #region Events
        protected override void OnShown(EventArgs e)
        {
            if (mSelectedAssetType != null)
            {
                txtAssetType.Text = mSelectedAssetType.AssetTypeName;
                txtAssetType.Tag = mSelectedAssetType;
            }
            BindComboBox();
            BindDataGrid();
            base.OnShown(e);
        }

        private void txtAssetType_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainAssetTypeQuery frm = new frmMaintainAssetTypeQuery();
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK && frm.SelectedAssetType != null)
            {
                txtAssetType.Text = frm.SelectedAssetType.AssetTypeName;
                txtAssetType.Tag = frm.SelectedAssetType;
            }
            else
            {
                txtAssetType.Text = "";
                txtAssetType.Tag = null;
            }
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_Confirm()
        {
            if (mSelectedAsset == null)
            {
                MessageBox.Show("请选择一个固定资产！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            this.DialogResult = DialogResult.OK;

        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedAsset = new FaAssetsEntity(dr);
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (mSelectedAsset == null)
            {
                return;
            }
            else
            {
                this.DialogResult = DialogResult.OK;
            }
        }
        #endregion

    }
}